<?php 
namespace Common\Service\Pay;
/**
 * 支付宝回调通知接口
 * @author liuyang
 *
 */  
use Common\ServiceInterface\PayNotifyService;
require_once("pay/alipay/lib/alipay_notify.class.php");
class AlipayNotifyCallbackService  implements PayNotifyService {
    
    private $config = array();
    
    public function __construct($config){   
        $this->config=$config;
    } 
       
    public function notify($callback){  
      //Log::write("POST:",json_encode($_REQUEST));
         $alipay_config = $this->config; 
         //echo $alipay_config['cacert'];
         //计算得出通知验证结果  
         $alipayNotify = new \AlipayNotify($alipay_config); 
         //两次请求确保，发现有时候验证不成功
         $verify_result = $alipayNotify->verifyNotify();  
         if(!$verify_result){
             echo "fail";
         }else{
                $trade_status = $_POST['trade_status'];
                $allData = $_REQUEST; 
                $trade_no = $_POST['trade_no']."|".$_POST['buyer_email'];
                $allData['transaction_id']=$trade_no;
                if($trade_status == 'TRADE_SUCCESS') {
                      if(call_user_func($callback,$allData)){
                          echo "success";
                      }else{ 
                          echo "fail";
                      }
                }else if ($trade_status== 'TRADE_FINISHED') { 
                    if(call_user_func($callback,$allData)){
                        echo "success";
                    }else
                        echo "fail";
                }else{
                    echo "fail";
                }
         }
         
     }
	   
}